﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Company
{
    public partial class CompanyCareList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCareCompanyList();
            }
        }

        #region 加载我关注的小组列表
        private void BindCareCompanyList()
        {
            Users uinfo = UserBase.GetCurrentUser();
            UserCareHelper uchelper = new UserCareHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            c.Add(CriteriaType.Equals, "ObjectType", 1);
            DataTable dt = uchelper.QueryUserCareBySearchToTable(c, null, 0, 0, null);
            dt.Columns.Add(new DataColumn("CompanyOwnerID"));
            dt.Columns.Add(new DataColumn("CompanyOwnerName"));
            dt.Columns.Add(new DataColumn("CompanyName"));
            dt.Columns.Add(new DataColumn("BlogPVNum"));
            dt.Columns.Add(new DataColumn("UserNum"));
            dt.Columns.Add(new DataColumn("CareNum"));
            dt.Columns.Add(new DataColumn("RealmName"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string companyid = dr["objectid"] + "";
                    //加载小组信息
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(companyid);
                    dr["CompanyOwnerID"] = cinfo.OwnerUserID;
                    dr["CompanyOwnerName"] = cinfo.OwnerNickName;
                    dr["CompanyName"] = cinfo.CompanyName;
                    dr["BlogPVNum"] = cinfo.BlogPVNum.ToString();
                    dr["UserNum"] = cinfo.UserNum.ToString();
                    dr["CareNum"] = cinfo.CareNum.ToString();
                    dr["RealmName"] = cinfo.RealmName.ToString();
                }

                lblCareCompany.Visible = false;
                rptCareCompanyList.DataSource = dt;
                rptCareCompanyList.DataBind();
            }
            else
            {
                lblCareCompany.Visible = true;
                rptCareCompanyList.Visible = false;
                lblCareCompany.Text = "<font color=\"red\">暂时没有关注的小组</font>";
            }

        }
        #endregion

        #region 关注的小组列表事件
        protected void rptCareCompanyList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id = hidID.Value;
            if (e.CommandName == "cancel")//取消关注该小组
            {
                UserCare ucinfo = new UserCare();
                UserCareHelper uchelper = new UserCareHelper();
                ucinfo = uchelper.GetUserCare(id);
                uchelper.DeleteUserCare(ucinfo);
                //小组关注的个数减少1
                Companys cinfo = new Companys();
                CompanysHelper chelper = new CompanysHelper();
                cinfo = chelper.GetCompanys(ucinfo.ObjectID);
                int carenum = cinfo.CareNum;
                carenum = carenum - 1;
                cinfo.CareNum = carenum;
                chelper.EditCompanys(cinfo);

                Tools.AlertAndRedirect("取消关注该小组成功", "CompanyCareList.aspx");
            }
        }
        #endregion
    }
}